﻿/*
 * File: BroadcastBallChaseCamera.cs
 * Author: Gourav Das
 * Purpose: Class for Broadcast chase camera
 * Created: June 2011
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _22yards.ThreeD
{
    public class BroadcastBallChaseCamera : BaseCamera
    {
        private bool chaseBall = true;
        public bool ChaseBall
        {
            get
            {
                return chaseBall;
            }
            set
            {
                chaseBall = value;
            }
        }

        public float fov
        {
            get
            {
                return FOV;
            }
            set
            {
                FOV = value;
            }
        }

        public BroadcastBallChaseCamera(Game game)
            : base(game)
        {
            ResetCamera();
        }

        public override void ResetCamera()
        {
            Position = new Vector3(0f, 90f/*0f*/, -380f);
            //Position = new Vector3(0f, 50, 49f);
            //Position = new Vector3(0f, 800f, 244f);
            
            rotationMatrix = Matrix.CreateFromYawPitchRoll(0, 0.23f, 0);
            //rotationMatrix = Matrix.CreateFromYawPitchRoll(0, 1.56f, 0);
            //rotationMatrix = Matrix.CreateFromYawPitchRoll(0, 0, 0);
            LookAt = new Vector3(0, 0f, 1f);
            transformedReference = Vector3.Transform(LookAt, rotationMatrix);
            LookAt = (Position + transformedReference);
            ViewMatrix = Matrix.Identity;
            FOV = 45.0f;
        }

        public void Update(GameTime _gametime, Vector3 chasedObjectsPos)
        {
            if (chaseBall)
            {
                LookAt = new Vector3(0, 0, 1);
                transformedReference = Vector3.Transform(LookAt, rotationMatrix);

                if (chasedObjectsPos.Z < -180 && chasedObjectsPos.Z > -480)
                    position.Z = chasedObjectsPos.Z - 80;
                    
                LookAt.X = Vector3.Lerp(new Vector3(LookAt.X,0,0),new Vector3(chasedObjectsPos.X + transformedReference.X,0,0),0.85f).X;
                if (chasedObjectsPos.Y > 80)
                {
                    LookAt.Y = Vector3.Lerp(new Vector3(0, LookAt.Y, 0), new Vector3(0, chasedObjectsPos.Y + transformedReference.Y, 0), 0.85f).Y;
                    position.Y = Vector3.Lerp(new Vector3(0, position.Y, 0), new Vector3(0, chasedObjectsPos.Y, 0), 0.45f).Y;
                }
                else
                {
                    LookAt.Y = Vector3.Lerp(new Vector3(0, LookAt.Y, 0), new Vector3(0, chasedObjectsPos.Y + transformedReference.Y, 0), 0.15f).Y;
                    position.Y = Vector3.Lerp(new Vector3(0, position.Y, 0), new Vector3(0, 90, 0), 0.85f).Y;
                }
                
                LookAt.Z = Vector3.Lerp(new Vector3(0,0,LookAt.Z),new Vector3(0,0,(chasedObjectsPos.Z + transformedReference.Z)),0.85f).Z;

                if (chasedObjectsPos.Z < -180)
                    FOV = Vector3.Lerp(new Vector3(FOV, 0, 0), new Vector3(50f, 0, 0), 0.015f).X;
                else if (chasedObjectsPos.Z > 230)
                    FOV = Vector3.Lerp(new Vector3(FOV, 0, 0), new Vector3(15f, 0, 0), 0.015f).X;
            }
            base.Update(_gametime);
        }
    }
}
